# coding:utf-8

import datetime
import os
import shutil
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup
from urllib.request import urlopen


def get_soup_html(url):
    try:
        result = requests.get(url)
        soup = BeautifulSoup(result.text, 'html.parser')
        return soup
    except Exception as e:
        print(e)

    print(url, 'download error')


def download_file(dir_path, file_name, url):
    """下载文件"""
    print("下载文件:", url)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    # 文件大小
    file_path = dir_path + file_name
    if os.path.exists(file_path):
        first_byte = os.path.getsize(file_path)
        return None
    else:
        first_byte = 0

    # 获取文件长度
    try:
        file_size = int(urlopen(url).info().get('Content-Length', -1))
    except Exception as e:
        print(e)
        print("错误，访问url: %s 异常" % url)
        return False

    if first_byte >= file_size:
        return file_size

    header = {"Range": "bytes=%s-%s" % (first_byte, file_size)}
    pbar = tqdm(
        total=file_size, initial=first_byte,
        unit='B', unit_scale=True, desc=url.split('/')[-1])

    r = requests.get(url, stream=True)
    f = open(file_path, "wb")
    for chunk in r.iter_content(chunk_size=512):
        if chunk:
            f.write(chunk)
            pbar.update(512)
    f.close()
    pbar.close()
    return True

# # 下载图片,参数为图片链接和来源网站
# def download_img(img_src, web_src):
#     # 定义文件夹的名字
#     imgName_list = img_src.split("/")
#     imgName = imgName_list[-1]
#
#     picpath = option['image_path'] + web_src  # 下载到的本地目录
#     if not os.path.exists(picpath):  # 路径不存在时创建一个
#         os.makedirs(picpath)
#     # 网站图片打不开时调用本地图片
#     try:
#         target = picpath + '\\%s' % imgName
#         if os.path.exists(target):
#             print('已经下载')
#             pass
#         else:
#             urllib.urlretrieve(img_src, target)  # 将远程数据下载到本地
#     except Exception as e:
#         print(e)
#     return target
#
#
# # 翻墙用
# def downloadImg_add(img_src_temp, web_src):
#     img_src = img_src_temp
#     # 定义文件夹的名字
#     imgName_list = img_src.split("/")
#     imgName = imgName_list[-1]
#     foldername = datetime.datetime.now().date().strftime('%Y%m%d')
#
#     # windows版本
#     if os.name == 'nt':
#         picpath = 'D:\\ImageDownload\\%s\\%s' % (web_src, foldername)  # 下载到的本地目录
#         if not os.path.exists(picpath):  # 路径不存在时创建一个
#             os.makedirs(picpath)
#         # 网站图片打不开时调用本地图片
#         try:
#             target = picpath + '\\%s' % imgName
#             image = urllib.urlretrieve(img_src, target)  # 将远程数据下载到本地
#         except Exception as e:
#             print(e)
#
#     # linux版本
#     elif os.name == 'posix':
#         picpath = '/root/ImageDownload/%s/%s' % (web_src, foldername)  # 下载到的本地目录
#         if not os.path.exists(picpath):  # 路径不存在时创建一个
#             os.makedirs(picpath)
#         # 网站图片打不开时调用本地图片
#         try:
#             target = picpath + '/%s' % imgName
#             image = urllib.urlretrieve(img_src, target)
#             # print 'Downloading image to location: ' + target
#         except Exception as e:
#             print(e)
#
#     return target
#
#
# if __name__ == "__main__":
#     get_soup_html("http://www.qstheory.cn/bwtj/index.htm")
